﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VOL.Core.Extensions;
using VOL.Core.Utilities;
using VOL.Entity.DomainModels;
using VOL.Entity.DomainModels.ProductionManagement;
using VOL.Smartmanufacturing.DTO.Bom;
using VOL.Smartmanufacturing.DTO.ProductionManagement;
using VOL.Smartmanufacturing.Infrastructure;
using VOL.Smartmanufacturing.IServices.ProductionManagement;

namespace VOL.Smartmanufacturing.Services.ProductionManagement
{
    public class EquipmentLedgerShow : IEquipmentLedgerShow
    {
        private readonly IBaseRepository<EquipmentLedgerModel> _baseRepository;
        /// <summary>
        /// 显示设备台账
        /// </summary>
        /// <param name="baseRepository"></param>
        public EquipmentLedgerShow(IBaseRepository<EquipmentLedgerModel> baseRepository)
        {
            _baseRepository = baseRepository;
        }

        public async Task<ApiPaging<EquipmentLedgerModel>> GetEquipmentLedger(EquipmentLedgerDTO equipmentLedgerDTO)
        {
            ApiPaging<EquipmentLedgerModel> res = new ApiPaging<EquipmentLedgerModel>();
            var equipmentLedgers = _baseRepository.GetAll();
            equipmentLedgers = equipmentLedgers.WhereIF(!string.IsNullOrEmpty(equipmentLedgerDTO.DeviceName), a => a.DeviceName.Contains(equipmentLedgerDTO.DeviceName));
            res.TotalCount = equipmentLedgers.Count();
            res.TotalPage = (int)Math.Ceiling(equipmentLedgers.Count() * 1.0/equipmentLedgerDTO.PageSize);
            res.Items = equipmentLedgers.OrderByDescending(a => a.Id).Skip((equipmentLedgerDTO.PageIndex - 1) * equipmentLedgerDTO.PageSize).Take(equipmentLedgerDTO.PageSize).ToList();
            return await Task.FromResult(res);

        }


    }
}
